package com.example.sixth.coree.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.sixth.coree.utils.MiscUtil;
import com.example.sixth.coree.entity.ReturnData;
import com.example.sixth.coree.utils.SpringUtils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RequestMapping("/api")
@RestController
@CrossOrigin
public class miscController {
    private static HashMap<String, Object> hashMap=new HashMap<>();
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    RedisTemplate redisTemplate;
    @RequestMapping("system/time")
    ReturnData getTime() {

        hashMap.put("time",System.currentTimeMillis());
        return new ReturnData(200,"fuck world",hashMap);
    }

    @RequestMapping("/sms/email")
    ReturnData sendEmail(@RequestBody Map mp){
        try{
        String email = (String) mp.get("email");
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1852974658@qq.com");
        message.setSubject("这是你的验证码");
        message.setTo(email);
        String s = RandomUtil.randomString(6);
        message.setText(s);
        javaMailSender.send(message);
            redisTemplate.opsForValue().set(email,s);
            redisTemplate.expire(email,10, TimeUnit.MINUTES);
            return new ReturnData(200,"验证码十分钟内有效 请注意查收","");
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnData(500,"输入正常的邮箱","");
    }
        @RequestMapping("attach/uploadimage")
        ReturnData uploadImage(@RequestParam("file") MultipartFile file,@RequestParam("access_token") String access_token)throws Exception{
            String s = file.getOriginalFilename().split("\\.")[1];
            new File("E://imgupload//" +s).mkdirs();
            String s1 = RandomUtil.randomString(13);
            String newfilename=s+"//"+ s1 + "." + s;
            File file1 = new File("E://imgupload//" +newfilename);
            long size = file.getSize();
            long fazhi=1024*1024;
            if(size<=fazhi){//压缩阈值
                file.transferTo(file1);
            }else{
                System.out.println(fazhi*1.0/size);
                try (FileOutputStream fileOutputStream = new FileOutputStream(file1)) {
                    Thumbnails.of(file.getInputStream()).scale(1f).outputFormat("jpg").outputQuality(Math.pow(fazhi * 1.0 / size,0.3)).toOutputStream(fileOutputStream);
                }



                System.out.println("压缩前："+size/1024.0/1024+"MB");
                System.out.println("压缩后："+file1.length()/1024.0/1024+"MB");
            }

            Map map=new LinkedHashMap();
            map.put("attach_status",0);
            map.put("attach_path",newfilename);
            map.put("attach_thumb",newfilename);
            map.put("attach_type",s);
            map.put("attach_size",file.getSize());
            map.put("attach_updatetime",System.currentTimeMillis());
            map.put("attach_createtime",System.currentTimeMillis());
            map.put("attach_user", MiscUtil.verifyToken(access_token));
            ReturnData res = new ReturnData(200, "上传成功", map);
            return res;

    }
    @RequestMapping("/attach/uploadHead")
    ReturnData uploadHead(@RequestParam("file") MultipartFile file,@RequestParam("access_token") String access_token)throws Exception{
        return uploadImage(file,access_token);
    }

}
